package xiangya.bysj.service.impl;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.taobao.api.domain.ImMsg;

import xiangya.bysj.entity.Cache;
import xiangya.bysj.mapper.interf.CacheMapper;
import xiangya.bysj.mapper.interf.CentMapper;
import xiangya.bysj.service.interf.CacheServiceInterface;
import xiangya.bysj.util.JsonUtil;
import xiangya.bysj.util.PropertyUtil;

@Component("cacheService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class CacheServiceImpl implements CacheServiceInterface {

	private static Properties prop;

	static {
		prop = PropertyUtil.loadFile("xiangya-temp.properties");

	}

	// 注入操作记录mapper
	@Resource
	private CacheMapper cacheMapper;

	@Resource
	private CentMapper centMapper;

	// 注入im服务类
	@Resource
	private IMServiceImpl imService;

	// 注入服药信息服务类
	@Resource
	private TakeInfoServiceImpl takeInfoService;

	/**
	 * 用户确认医师修改的服药提醒
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void confirm(int fid) throws Exception {

		Cache cache = cacheMapper.selectByStatus(fid);
		if (cache == null)
			throw new RuntimeException("该记录已失效");
		// 取出操作记录
		String json = cache.getOperation();
		// 解析json;进行操作
		/**
		 * 传过来的json格式 { "oper":"save/saveCycle/delete/delteCycle/update"
		 * "data":json of TakeInfo/id of int to delete }
		 */
		// 通过反射进行操作
		// String json=
		// "{"
		// +"\"oper\":\"delete\","
		// +"\"data\":\"1\""
		// +"}";
		//
		Map<String, Object> map = (Map<String, Object>) JsonUtil.getObject(json, Map.class);
		String oper = (String) map.get("oper");
		String data = JsonUtil.getJson(map.get("data"));
		Class<TakeInfoServiceImpl> clazz = (Class<TakeInfoServiceImpl>) Class
				.forName("xiangya.bysj.service.impl.TakeInfoServiceImpl");
		Method method = clazz.getMethod(oper, Class.forName(prop.getProperty(oper)));
		if (!oper.equals("delete")) {
			method.invoke(takeInfoService, JsonUtil.getSelfObject(data, Class.forName(prop.getProperty(oper))));
		} else {
			method.invoke(takeInfoService, JsonUtil.getObject(data, Class.forName(prop.getProperty(oper))));
		}

		// 修改状态为已确认
		cache.setStatus(2);
		cacheMapper.updateStatus(cache);

		// 修改积分
		centMapper.updateCent("forUpdate", cache.getDoctorAccount(), Double.parseDouble(prop.getProperty("forUpdate")));

	}

	/**
	 * 保存医师修改服药提醒
	 * 
	 * @param cache
	 *            操作记录实体类
	 * @param m
	 *            要发送给用户的提醒消息
	 * @throws Exception
	 */
	@Override
	public int saveUpdateTakeinfos(Cache cache, ImMsg m) throws Exception {
		// 保存操作信息
		cacheMapper.save(cache);
		// 发送im信息给用户,等待确认
		// imService.sendMsg(m);
		return cache.getFid();
	}

	/**
	 * 用户取消医师修改的服药提醒
	 */
	@Override
	public void cancel(int fid) throws Exception {

		Cache cache = new Cache();
		if (cache == null)
			throw new RuntimeException("该记录已失效");
		cache.setFid(fid);
		// 修改记录为已撤销
		cache.setStatus(3);
		cacheMapper.updateStatus(cache);
	}

	public List<Cache> getCacheByUserAccount(String userAccount) {
		return cacheMapper.selectCacheByUseraccount(userAccount);
	}

	@Override
	public void delete(int objId) throws Exception {
	}

	@Override
	public void save(Cache obj) throws Exception {
	}

	@Override
	public void update(Cache obj) throws Exception {
	}

}
